package whereRU.frontEnd;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

/**
 * Class that creates a TimePickerDialog object and binds it to a button
 */
public class TimePickerButton extends DialogFragment
		implements TimePickerDialog.OnTimeSetListener {

	private boolean isSet = false;
	private SimpleTime sTime;
	private Button button;

	/**
	 * Constructor
	 * @param button the button to bind it to
	 * @param fragmentActivity the activity this dialog belongs to
	 */
	public TimePickerButton(Button button, final FragmentActivity fragmentActivity) {
		sTime = new SimpleTime();	
		this.button = button;
		
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				show(fragmentActivity.getSupportFragmentManager(), "timepicker");
			}
		});
	}
	
	/**
	 * Creates the time picker dialog
	 */
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Create a new instance of TimePickerDialog and return it
		return new TimePickerDialog(getActivity(), this, sTime.getHours(), sTime.getMinutes(),
				DateFormat.is24HourFormat(getActivity()));
	}

	/**
	 * Once the time has been selected, update the hour, minute, and button text
	 */
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		sTime.setHours(hourOfDay);
		sTime.setMinutes(minute);
		sTime.calculateTotalTime();
		button.setText(sTime.toString());
		isSet = true;
	}
	
	/**
	 * Returns the SimpleTime object
	 * @return SimpleTime object
	 */
	public SimpleTime getSimpleTime() {	return sTime;	};
	
	/**
	 * Shows if the time has been set
	 * @return true if the time has been set, otherwise false
	 */
	public boolean isSet() {
		return isSet;
	}
	
	/**
	 * Sets the time
	 * @param hourOfDay	the hour of day
	 * @param minute	the minute
	 */
	public void setTime(int hourOfDay, int minute) {
		sTime.setHours(hourOfDay);
		sTime.setMinutes(minute);
		sTime.calculateTotalTime();
		button.setText(sTime.toString());
		isSet = true;
	}
}